%Cross validation and actual logistic regression are both performed in this
%file.
load spamData.mat;
Xmean = mean(Xtrain);
Xstd = std(Xtrain);
Xstandard = Xtrain;

%Standardize train data
for rowIndex=1:size(Xstandard,1)
    for colIndex=1:size(Xstandard,2)
        Xstandard(rowIndex,colIndex) = (Xstandard(rowIndex,colIndex) - Xmean(colIndex))/Xstd(colIndex);
    end
end


Xmean = mean(Xtest);
Xstd = std(Xtest);
XstandardTest = Xtest;

%Standardize test data
for rowIndex=1:size(XstandardTest,1)
    for colIndex=1:size(XstandardTest,2)
        XstandardTest(rowIndex,colIndex) = (XstandardTest(rowIndex,colIndex) - Xmean(colIndex))/Xstd(colIndex);
    end
end

%Add column of 1s to represent X0
Xstandard(:,size(Xstandard,2)+1) = 1;
XstandardTest(:,size(XstandardTest,2)+1) = 1;
    
%Get the w vector using the logistic regression function posted by Marcos
%Lambda is set to 0.2 (optimal lambda from cross validation)
lamda=0.2
weights=logregnew(Xstandard,ytrain,lamda)

%Get the probabilities for the training set
trainProb = zeros(size(Xstandard,1),1);
for trainIndex=1:size(Xstandard,1)
    trainProb(trainIndex) = 1/(1+exp(-weights' * Xstandard(trainIndex,:)'));
end

%Get the probabilities for the test set
testProb = zeros(size(XstandardTest,1),1);
for testIndex=1:size(XstandardTest,1)
    testProb(testIndex) = 1/(1+exp(-weights' * XstandardTest(testIndex,:)'));
end
trainResult = round(trainProb);
testResult = round(testProb);

%Compute training and test errors
trainError = sum(trainResult ~= ytrain)/size(ytrain,1)
testError = sum(testResult ~= ytest)/size(ytest,1)

%Concatenate and randomize the training data for cross validation purposes.
xtotal = cat(2,Xstandard,ytrain);
xtotal = xtotal(randperm(size(xtotal,1)),:);
x = xtotal(:,1:58);
y = xtotal(:,59);

%Do 5 fold cross validation with the lambdaValues below
lambdaValues = [0.2:0.2:1 2:2:10];
crossvalidateresults = zeros(size(lambdaValues,2),2);

%Print out results from cross validation
for i=1:size(lambdaValues,2)
    lambda = lambdaValues(i)
    crossvalidateresults(i,1) = lambda;
    crossvalidateresults(i,2)= crossvalidate(x,y,lambda)
end
plot(crossvalidateresults(:,1),crossvalidateresults(:,2));
